<?php
// +----------------------------------------------------------------------
// | ZGUAN [ 致力于帮助更多的中小企业物联网转型 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017~2024 https://www.zguan.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ZGUAN 并不是自由软件，未经许可不能去掉ZGUAN 相关版权
// +----------------------------------------------------------------------
// | Author: ZGUAN Team <admin@zguan.cn>  0769-82282996  15899929162
// +----------------------------------------------------------------------
declare (strict_types=1);

namespace app\http\middleware\api;

use app\exception\ApiException;
use app\http\response\api\StatusCode;
use app\common\repositories\property\basic\PropertyBasicRepository;

class CheckProperty
{

    public function handle($request, \Closure $next)
    {
        $keyCode = $request->header('Key-code');
        if (!$keyCode) {
            throw new ApiException('物业信息错误', StatusCode::LOGIN_CODE);
        }
        /**
         * @var PropertyBasicRepository $propertyBasicRepository
         */
        $propertyBasicRepository = app()->make(PropertyBasicRepository::class);
        if (!($propertyInfo = $propertyBasicRepository->getByKeyCode($keyCode))) {
            throw new ApiException('物业不存在', StatusCode::LOGIN_CODE);
        }
        $request->propertyId = $propertyInfo['id'];
        return $next($request);
    }
}